package com.gzx.spring.drools.controller;

import com.alibaba.fastjson2.JSON;
import com.gzx.risk.core.pojo.CommonResult;
import com.gzx.risk.core.pojo.UserLevelResultInfo;
import com.gzx.risk.core.service.UserLevelService;
import com.gzx.spring.drools.provider.RiskUserLevelFeign;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@Tag(name = "Score")
@RestController
@Validated
@RequestMapping("/feign/risk")
@Slf4j
public class UserLevelController implements RiskUserLevelFeign {
    @Resource
    private UserLevelService userLevelService;

    @Override
    @PostMapping("/userScore/rate")
    public void rateUserScore(@RequestParam(name = "uid")Integer uid,
                              @RequestParam(name = "oid")Integer oid) throws Exception {
        log.info("rateUserScore uid={}, oid={}", uid, oid);
        userLevelService.rateUserScore(uid,oid);
    }

    @PostMapping("/getUserScoreInfo")
    public String getUserScoreInfo(@RequestParam(name = "uid") String uid) {
        CommonResult<Object> result = new CommonResult<>();
        if (StringUtils.isEmpty(uid)) {
            result = CommonResult.error().setMsg("参数为空");
            return JSON.toJSONString(result);
        }

        UserLevelResultInfo resultInfo = userLevelService.getUserScoreByUid(uid);
        if (resultInfo == null) {
            result = CommonResult.error().setMsg("该用户未分层");
        }else {
            result = CommonResult.ok().setData(resultInfo);
        }
        return JSON.toJSONString(result);
    }
}
